package com.ujiuye.servlet;

import com.ujiuye.bean.StudentBean;
import com.ujiuye.service.StudentService;
import com.ujiuye.utils.FileUploadUtils;
import com.ujiuye.utils.PageUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/student")
@MultipartConfig
public class StudentServlet extends BaseServlet {
    private StudentService ss = new StudentService();

    //添加学员
    public void addStudent(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取页面传递过来的数据
        String sname = req.getParameter("sname");
        String sex = req.getParameter("sex");
        String age = req.getParameter("age");
        String degree = req.getParameter("degree");
        String mark = req.getParameter("mark");

        Part part= req.getPart("pic");
        String pic = FileUploadUtils.uploaPic(part);

        //封装学生对象
        StudentBean stu = new StudentBean(sname, sex, Integer.parseInt(age), degree, pic, mark);
        //调用service层的添加方法
        int row = ss.addStudent(stu);
        PrintWriter out=resp.getWriter();
        if (row>0){
//            out.print("添加成功");
            //跳转到列表页面从新执行查询方法
            resp.sendRedirect("student?method=findAllStudent");
        }else {
//            out.print("添加失败");
            //直接返回页面
            resp.sendRedirect("addStudent.jsp");
        }
    }
    public void findAllStudent(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
       //获取总记录数
        int totalCount = ss.getTotalCount();
        //从页面获取当前页码
        String currentPage = req.getParameter("currentPage");
        //创建分页查询工具类对象
        PageUtils pu = new PageUtils(totalCount, 8, currentPage);
        //调用service层查询方法
        List<StudentBean> list= ss.findAllStudentByPage(pu);
        //将分页查出来的数据保存到分页工具类对象的集合中
        pu.setList(list);
        //将查询到的数据保存到域中
        req.setAttribute("pu",pu);
        //转发到index.jsp页面
        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }
    //转发至修改页面
    public  void detailedFindStudentBySid(HttpServletRequest req,HttpServletResponse resp)throws Exception{
        //获取学号
        String sid=req.getParameter("sid");
        //调用service
        StudentBean student=ss.findStudentBySid(Integer.parseInt(sid));
        //存域
        req.setAttribute("stu",student );
        //跳转
        req.getRequestDispatcher("updatestudent.jsp").forward(req,resp );

    }
    //删除
    //根据学号查询sid信息
    public  void findStudentBySid(HttpServletRequest req,HttpServletResponse resp)throws Exception{
        //获取学号
        String sid=req.getParameter("sid");
        //调用service
        StudentBean student=ss.findStudentBySid(Integer.parseInt(sid));
        //存域
        req.setAttribute("stu",student );
        //跳转
        req.getRequestDispatcher("details.jsp").forward(req,resp );

    }
//    修改
    public void updateStudent(HttpServletRequest req,HttpServletResponse resp)throws Exception{
      //获取页面数据
        String sname = req.getParameter("sname");
        String sex = req.getParameter("sex");
        String age = req.getParameter("age");
        String degree = req.getParameter("degree");
        String pic = req.getParameter("oldpic");//旧头像
        String mark = req.getParameter("mark");
        String sid = req.getParameter("sid");
        //上传
        Part part =req.getPart("pic");
        //判断是否选择新头像
        if (part.getSize()!=0){
            pic=FileUploadUtils.uploaPic(part);
        }

        //封装对象
        StudentBean student=new StudentBean(Integer.parseInt(sid),sname,sex,Integer.parseInt(age),degree,pic,mark);
        //调用service
        int row = ss.updateStudent(student);
        //判断
        if (row>0){
            resp.sendRedirect("student?method=findAllStudent");
        }
    }
    //删除
    public void deleteStudent(HttpServletRequest req,HttpServletResponse resp)throws Exception{
       //获取学号
        String sid=req.getParameter("sid");
        //调用service
        int st = ss.deleteStudent(Integer.parseInt(sid));
        //存域
        req.setAttribute("stu",st);
        //跳转
        req.getRequestDispatcher("student?method=findAllStudent").forward(req,resp );

    }
    public void checkSname(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        //获取姓名
        String sname = req.getParameter("sname");
        //调用service查询方法
        StudentBean stu = ss.checkSname(sname);
        PrintWriter writer = resp.getWriter();
        //判断是否存在
        if(stu==null){
            writer.print(false);
        }else {
            writer.print(true);
        }
    }
    
}

